package com.mdlib.helper.module.send.order.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.core.util.common.KeyboardUtils;
import com.ljlib.core.util.common.RegularUtils;
import com.ljlib.help.R;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.event.PhoneEvent;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/21 16:38
 * 邮箱：hqy_xz@126.com
 * 修改手机号
 */

public class ModifyPhoneFragment extends BaseTitleFragment {


    @Bind(R.id.et_modify_phone)
    EditText mEtModifyPhone;
    @Bind(R.id.tv_modify_confirm)
    TextView mTvModifyConfirm;
    private String phoneNumber;
    private String type;

    public static ModifyPhoneFragment newInstance(String phoneNumber, String type) {
        ModifyPhoneFragment fragment = new ModifyPhoneFragment();
        Bundle bundle = new Bundle();
        bundle.putString(UIHelper.PHONE, phoneNumber);
        bundle.putString(UIHelper.TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            phoneNumber = bundle.getString(UIHelper.PHONE);
            type = bundle.getString(UIHelper.TYPE);
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        KeyboardUtils.hideSoftInput(getActivity(), mEtModifyPhone);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_modify_phone;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.modify_phone_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();
    }

    public void init() {
        mEtModifyPhone.setText(phoneNumber);
    }


    @OnClick(R.id.tv_modify_confirm)
    public void onClick() {
        String phoneNumber = mEtModifyPhone.getText().toString();
        if (EmptyUtils.isEmpty(phoneNumber)) {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_not_empty));
            return;
        }
        if (RegularUtils.isMobileExact(phoneNumber)) {
            EventBus.getDefault().post(new PhoneEvent(phoneNumber, type));
            removeFragment();
        } else {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_phone_form));
        }
    }
}
